clc
clear all
close all

dateCount=1;
startDate=datetime([2022,11,22,0,0,0]);
stopDate=datetime([2022,11,22,23,59,59]);

for i=1:dateCount
    startDateString=datestr(startDate,'yyyy-mm-dd HH:MM:SS');
    stopDateString=datestr(stopDate,'yyyy-mm-dd HH:MM:SS');    
    %读取数据
    data=GetWaterKey(startDateString,stopDateString);
   
    %表转为矩阵进行计算，第一列时间单独提出来,data后续列-1
    time = data.time;
    DataArray = table2array(data(:,2:end));
    
    %初始化
    BallRatio = zeros(length(DataArray),1);         %成球率
    SchemeStatus = zeros(length(DataArray),1);      %内配是否改变
    TotalStatus = zeros(length(DataArray),1);       %总料量是否改变
    OnlyReturnStatus = zeros(length(DataArray),1);  %单压返料
    WaterChange = zeros(length(DataArray),1);       %水是否改变
    PreviousWater = zeros(length(DataArray),1);
    CurrentA = zeros(length(DataArray),1);
    CurrentB = zeros(length(DataArray),1);
    CurrentA_1min = zeros(length(DataArray),1);
    CurrentA_2min = zeros(length(DataArray),1);
    CurrentA_3min = zeros(length(DataArray),1);
    CurrentB_1min = zeros(length(DataArray),1);
    CurrentB_2min = zeros(length(DataArray),1);
    CurrentB_3min = zeros(length(DataArray),1);
    ReturnBin_10min = zeros(length(DataArray),1);
    Z103A = zeros(length(DataArray),1);
    Z103B = zeros(length(DataArray),1);
    curret_a_average_3 = zeros(length(DataArray),1);
    curret_b_average_3 = zeros(length(DataArray),1);


% % 计算平均值
% data_aver = (data2(:,1)+data2(:,2))/2;


    %对成球率、内配改变、总料量改变、单压返料、掺水量的标志位进行向前取值并计算
    for i = 1:length(DataArray)
        %前五分钟单独考虑，数据前五分钟作废
        if i <= 60
            BallRatio(i) = DataArray(i,5)./(DataArray(i,3)+DataArray(i,4));
        else
            %成球率回溯90s计算
            BallRatio(i) = DataArray(i,5)./(DataArray(i-18,3)+DataArray(i-18,4));
            %内配是data第14列到倒数第二列
            SchemeStatus(i) = ~isequal(DataArray(i,13:end-1),DataArray(i-12,13:end-1));
            %总料量设定是最后一列
            TotalStatus(i) = ~isequal(DataArray(i,end),DataArray(i-12,end));
            %返料仓是data第13列,有点问题,再考虑考虑
            %OnlyReturnStatus(i) = ~isequal(DataArray(i,12),DataArray(i-12,12));
            %掺水量的标志位,data第2列
            WaterChange(i) = ~isequal(DataArray(i,1),DataArray(i-1,1));
            PreviousWater(i) = DataArray(i-1,1);
        end
    end  

    %对成球率和电流分别进行预处理和滤波
    %定义窗口大小,数值越大滤波效果越大
    BallRatio = SmoothFilter(BallRatio,0.30,0.95,60);
    %压球电流:第七行第八行
    CurrentA = SmoothFilter(DataArray(:,6),100,200,60);
    CurrentB = SmoothFilter(DataArray(:,7),100,200,60);
    Z103A = DataArray(:,3);
    Z103B = DataArray(:,4);
%% 新建数据

    temp_data = [CurrentA, CurrentB,Z103A,Z103B];
    temp_data(find(temp_data(:,1:2)<=120 & temp_data(:,3:4)<3)) = NaN;
    n = length(temp_data);
    % 3h
    for i = 1:n
        if i <= 2160
            curret_a_average_3(i) = CurrentA(i,1);
            curret_b_average_3(i) = CurrentB(i,1);
        else
            curret_a_average_3(i) = nanmean(temp_data(i-2160:i,1));
            curret_b_average_3(i) = nanmean(temp_data(i-2160:i,2));
        end
    end


%%
    %电流和返料仓趋势
    for i = 1:length(DataArray)
        %前10分钟单独考虑，数据前10分钟作废
        if i <= 120
            CurrentA_1min(i) = CurrentA(i)-CurrentA(1);
            CurrentA_2min(i) = CurrentA(i)-CurrentA(1);
            CurrentA_3min(i) = CurrentA(i)-CurrentA(1);
            CurrentB_1min(i) = CurrentB(i)-CurrentB(1);
            CurrentB_2min(i) = CurrentB(i)-CurrentB(1);
            CurrentB_3min(i) = CurrentB(i)-CurrentB(1);
            ReturnBin_10min(i) = DataArray(i,12)-DataArray(1,12);
        else
            CurrentA_1min(i) = CurrentA(i)-CurrentA(i-12);
            CurrentA_2min(i) = CurrentA(i)-CurrentA(i-24);
            CurrentA_3min(i) = CurrentA(i)-CurrentA(i-36);
            CurrentB_1min(i) = CurrentB(i)-CurrentB(i-12);
            CurrentB_2min(i) = CurrentB(i)-CurrentB(i-24);
            CurrentB_3min(i) = CurrentB(i)-CurrentB(i-36);
            ReturnBin_10min(i) = DataArray(i,12)-DataArray(i-120,12);
        end
    end

    %找出掺水量改变点
    WaterIndex = find(WaterChange==1); 
    NegativeSampleIndex=sort(randi([1,size(DataArray,1)],length(WaterIndex),1));
    WaterIndex=[WaterIndex;NegativeSampleIndex];

    %开始制作数据集
    %[1].时间戳
    time = time(WaterIndex);
    %[2].之前的掺水量PreviousWater
    PreviousWater = PreviousWater(WaterIndex);
    %[3].此时的掺水量NowWater
    NowWater = DataArray(WaterIndex,1);
    %[4].调水标签WaterChange
    WaterChange = WaterChange(WaterIndex);
    %[5].返料仓
    ReturnBin = DataArray(WaterIndex,12);
    %[6].返料仓10min趋势
    ReturnBin_10min = ReturnBin_10min(WaterIndex);
    %[7-10].压球电流A及其三个趋势
    CurrentA = CurrentA(WaterIndex);
    CurrentA_1min = CurrentA_1min(WaterIndex);
    CurrentA_2min = CurrentA_2min(WaterIndex);
    CurrentA_3min = CurrentA_3min(WaterIndex);
    %[11-14].压球电流B及其趋势
    CurrentB = CurrentB(WaterIndex);
    CurrentB_1min = CurrentB_1min(WaterIndex);
    CurrentB_2min = CurrentB_2min(WaterIndex);
    CurrentB_3min = CurrentB_3min(WaterIndex);
    %[15,16]A、B转速设定
    SpeedSetA = DataArray(WaterIndex,8);
    SpeedSetB = DataArray(WaterIndex,9);
    %[17,18]A、B缓冲仓料位
    BufferBinA = DataArray(WaterIndex,10);
    BufferBinB = DataArray(WaterIndex,11);
    %[19,20]总料及其标志位
    TotalWeight = DataArray(WaterIndex,end);
    TotalStatus = TotalStatus(WaterIndex);
    %[21]内配改变标志位
    SchemeStatus = SchemeStatus(WaterIndex);




    %[22]电流A_3小时平均值
    curret_a_average_3 = curret_a_average_3(WaterIndex);
    %[23]电流B_3小时平均值
    curret_b_average_3 = curret_b_average_3(WaterIndex);

    %构建新表并保存
    WaterTable = table(time,PreviousWater,NowWater,WaterChange,ReturnBin,ReturnBin_10min,CurrentA,CurrentA_1min,...
        CurrentA_2min,CurrentA_3min,CurrentB,CurrentB_1min,CurrentB_2min,CurrentB_3min,SpeedSetA,SpeedSetB,BufferBinA,...
        BufferBinB,TotalWeight,TotalStatus,SchemeStatus,curret_a_average_3,curret_b_average_3);
    csvFileName=strrep([startDateString,'.csv'],':','_');
    writetable(WaterTable,csvFileName);

    startDate=startDate+day(1);
    stopDate=stopDate+day(1);
end